// Copyright 2014, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.api.ads.dfp.jaxws;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import com.google.api.ads.common.lib.testing.MockHttpIntegrationTest;
import com.google.api.ads.dfp.jaxws.factory.DfpServices;
import com.google.api.ads.dfp.jaxws.testing.SoapRequestXmlProvider;
import com.google.api.ads.dfp.jaxws.v201702.Company;
import com.google.api.ads.dfp.jaxws.v201702.CompanyServiceInterface;
import com.google.api.ads.dfp.lib.client.DfpSession;
import com.google.api.ads.dfp.lib.soap.testing.SoapResponseXmlProvider;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.common.collect.Lists;
import org.custommonkey.xmlunit.XMLAssert;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.List;
import javax.xml.ws.WebServiceException;
/**
* Tests that a DFP JAX-WS SOAP call can be made end-to-end.
*/
@RunWith(JUnit4.class)
public class DfpJaxWsSoapIntegrationTest extends MockHttpIntegrationTest {
private static final String API_VERSION = "v201702";
@Rule
public final ExpectedException thrown = ExpectedException.none();
@BeforeClass
public static void setupClass() {
System.setProperty("api.adwords.useCompression", "false");
}
/**
* Tests making a JAX-WS DFP API call with OAuth2.
*/
@Test
public void testGoldenSoap_oauth2() throws Exception {
testHttpServer.setMockResponseBody(SoapResponseXmlProvider.getTestSoapResponse(API_VERSION));
GoogleCredential credential = new GoogleCredential.Builder().setTransport(
new NetHttpTransport()).setJsonFactory(new JacksonFactory()).build();
credential.setAccessToken("TEST_ACCESS_TOKEN");
DfpSession session = new DfpSession.Builder().withApplicationName("TEST_APP")
.withOAuth2Credential(credential)
.withEndpoint(testHttpServer.getServerUrl())
.withNetworkCode("TEST_NETWORK_CODE")
.build();
CompanyServiceInterface companyService =
new DfpServices().get(session, CompanyServiceInterface.class);
List<Company> companies = companyService.createCompanies(Lists.newArrayList(new Company()));
assertEquals(1234L, companies.get(0).getId().longValue());
XMLAssert.assertXMLEqual(SoapRequestXmlProvider.getOAuth2SoapRequest(API_VERSION),
testHttpServer.getLastRequestBody());
assertFalse("Did not request compression but request was compressed",
testHttpServer.wasLastRequestBodyCompressed());
assertEquals("Bearer TEST_ACCESS_TOKEN", testHttpServer.getLastAuthorizationHttpHeader());
}
/**
* Tests that the request timeout in ads.properties is enforced.
*/
@Test
public void testRequestTimeoutEnforced() throws Exception {
System.setProperty("api.dfp.soapRequestTimeout", "100");
testHttpServer.setMockResponseBody(SoapResponseXmlProvider.getTestSoapResponse(API_VERSION));
testHttpServer.setDelay(200);
GoogleCredential credential = new GoogleCredential.Builder().setTransport(
new NetHttpTransport()).setJsonFactory(new JacksonFactory()).build();
credential.setAccessToken("TEST_ACCESS_TOKEN");
DfpSession session = new DfpSession.Builder().withApplicationName("TEST_APP")
.withOAuth2Credential(credential)
.withEndpoint(testHttpServer.getServerUrl())
.withNetworkCode("TEST_NETWORK_CODE")
.build();
CompanyServiceInterface companyService =
new DfpServices().get(session, CompanyServiceInterface.class);
thrown.expect(WebServiceException.class);
thrown.expectMessage("Read timed out");
companyService.createCompanies(Lists.newArrayList(new Company()));
}
}